<?php
class Jwawa_Db_SearchBuilder
{
    private $searchPageCode;
    private $searchItems;
    private $start;
    private $limit;
    private $stSearchItemList;

    public function getSearchItems(){
        return $this->searchItems;
    }

    public function getSearchPageCode(){
        return $this->searchPageCode;
    }

    public function getStart(){
        return $this->start;
    }

    public function getLimit(){
        return $this->limit;
    }

    public function __construct($searchPageCode, &$searchItems, $start = null, $limit  = null)
    {
        $this->searchPageCode = $searchPageCode;
        $this->searchItems = $searchItems;
        $this->start = $start;
        $this->limit = $limit;

        $searchItemNameArray = array_keys($this->searchItems);
        $this->stSearchItemList = Model_Cfg_SearchItem::findSearchItemByPgCodeWithNameLimit($this->searchPageCode,
                                                                                      $searchItemNameArray);
    }

    public function build($isCount = false){
        $rst = '';
        foreach($this->stSearchItemList as $stSearchItem){
            if(!empty($this->searchItems[$stSearchItem->entity_field_name])){
                $rst .= $this->buildParam($stSearchItem->entity_field_name,
                                          $stSearchItem->condition_flag,
                                          $stSearchItem->item_type,
                                          $stSearchItem->sql_field);
            }
        }

        if(!$isCount && is_numeric($this->start) && is_numeric($this->limit)){
            $rst .= sprintf(' LIMIT %d, %d', $this->start, $this->limit);
        }

        return $rst;
    }

    private function buildParam($paramName, $conditionFlag, $itemType, $sql_field){
        $rst2 = null;

        switch($conditionFlag){
            case 'equal':
                $rst2 = " AND $sql_field = :$paramName ";
                break;
            case 'like':
                $paramValue = $this->searchItems[$paramName];
                $rst2 = " AND $sql_field LIKE '%$paramValue%' ";
                break;
            case 'between':
                $subParamValue = explode(";", $this->searchItems[$paramName]);
                $paramNameFrom = $paramName . '_from';
                $paramNameTo = $paramName . '_to';
                if(count($subParamValue) == 2){
                    if($itemType == 'date'){
                        $this->searchItems[$paramNameFrom] = $subParamValue[0] . ' 00:00:00';
                        $this->searchItems[$paramNameTo] = $subParamValue[1] . ' 24:00:00';
                    } else {
                        $this->searchItems[$paramNameFrom] = $subParamValue[0];
                        $this->searchItems[$paramNameTo] = $subParamValue[1];
                    }
                } else if(count($subParamValue) == 1){
                    if($itemType == 'date'){
                        $this->searchItems[$paramNameFrom] = $subParamValue[0] . ' 00:00:00';
                        $this->searchItems[$paramNameTo] = $subParamValue[0] . ' 24:00:00';
                    } else {
                        $this->searchItems[$paramNameFrom] = $subParamValue[0];
                        $this->searchItems[$paramNameTo] = $subParamValue[0];
                    }
                }
                $rst2 .= " AND $sql_field >= :$paramNameFrom AND $sql_field <= :$paramNameTo ";
                break;
        }

        return $rst2;
    }
}
